<?php
/**
 * @author Erling Owe <erling.owe@gmail.com>
 * @package Core
 * @subpackage Controllers
 */ 

namespace Core\Controllers;

use \Core\StringHandling as StringHandling;

/**
 * Handles the request and dispatches an answer.
 *
 * This class is a singleton
 */
final class Front {
   
    private static $objInstance;

    private $cstrURL;
    private $intCurrentPathSegment = 0;


    function __construct () {
      
        session_start();

        // Get the request URI.
        $this->cstrURL = new StringHandling\URL ( StringHandling\REQUEST_URI );
    
    }

    /**
     * Gets an instance of itself.
     *
     * @return Core\Controllers\Front
     */
    public static function getInstance () {
      
        if ( !self::$objInstance instanceof self )
            self::$objInstance = new self;
      
        return self::$objInstance;
      
    }

    private function currentPathSegment () {
        return $this->cstrURL->PathSegments[$this->intCurrentPathSegment];
    }

    private function nextPathSegment () {
        ++$this->intCurrentURLPathSegment;
    }

    public function process () {

        switch ( $this->currentPathSegment() ) {

            default:

                $Paths = \Core\Paths::getInstance();
                
                $strName = $this->currentPathSegment();
                $strFilename = $Paths->Controllers . $strName . "Controller.php";

                if ( file_exists( $strFilename ) ) {
                    $strControllerName = $strName;
                    $strControllerFilename = $strFilename;
                    $this->nextPathSegment();
                } else {
                    $strControllerName = "Index";
                    $strControllerFilename = $Paths->Controllers . "IndexController.php";
                    // TODO: Throw an error if {CONTROLLERS}/IndexController.php does not exist.
                }


                require_once $strControllerFilename;

                $strControllerClassname = $strControllerName . "Controller";

                $strActionName = ( method_exists( $strControllerClassname, $this->currentPathSegment() . "Action" ) ) ? $this->currentPathSegment() : "Index";
                // TODO: Throw an error if the method IndexAction does not exist.

                $ActionController = new $strControllerClassname ( $strControllerName, $strActionName );

                $strActionMethodName = $strActionName . "Action";
                $ActionController->$strActionMethodName();
                $ActionController->View->dispatch();

        }
      
    }

/* Force download.   
header("Content-Disposition: attachment; filename=" . urlencode($file));    
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");             
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.

$fp = fopen($file, "r"); 
while (!feof($fp))
{
    echo fread($fp, 65536); 
    flush(); // this is essential for large downloads
}  
fclose($fp); 
*/   
   
}

?>
